<template>
  <a-modal
  class="withdraw-audit-modal"
  :visible="visible"
  :title="title"
  :maskClosable="false"
  :destroyOnClose="true"
  :keyboard="false"
  :closable="false"
  :confirmLoading="confirmLoading"
  width="70%"
  @cancel="closeModal(false)"
  @ok="handleSubmit">
    <a-form-model
      v-if="handle == 'edit'"
      class="withdraw-audit-form"
      ref="ruleForm"
      :model="formData"
      :rules="rules"
      :label-col="{ span: 8 }"
      :wrapper-col="{ span: 16 }">
      <a-form-model-item label="审核结果" prop="auditorStatus">
        <a-radio-group
          button-style="solid"
          v-model="formData.auditorStatus"
          :disabled="handle == 'view'">
          <a-radio-button :value="2">同意</a-radio-button>
          <a-radio-button :value="3">拒绝</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="审核意见" prop="remark">
        <a-textarea
          style="width: 400px"
          rows="4"
          placeholder="请输入审核意见"
          v-model="formData.remark"
          :disabled="handle == 'view'"
          :maxLength="200" />
      </a-form-model-item>
    </a-form-model>
    <a-descriptions style="margin-bottom: 20px" v-if="handle == 'view'" title="" bordered :column="2">
      <a-descriptions-item label="审核结果">
        {{ formData.auditorStatusStr }}
      </a-descriptions-item>
      <a-descriptions-item label="审核意见">
        {{ formData.remark }}
      </a-descriptions-item>
      <a-descriptions-item label="审核时间">
        {{ formData.updateDate }}
      </a-descriptions-item>
      <a-descriptions-item label="审核人">
        {{ formData.auditor }}
      </a-descriptions-item>
    </a-descriptions>
    <search-table
      :url="url"
      ref="searchtable"
      :hideSelect="true"
      :columns="columns"
      :defaultParams="defaultParams"
      :headParams="headParams"
      :form="form">
      <!-- 搜索条件 -->
      <template slot="searchForm">
        <a-form layout="inline" :form="form">
          <a-form-item label="账单时间">
            <fast-range-picker
              style="width: 240px"
              v-decorator="['startTime-endTime']">
            </fast-range-picker>
          </a-form-item>
          <!-- <a-form-item label="交易类型">
            <a-select
              showSearch
              allowClear
              style="width: 150px"
              :options="dealTypeList"
              v-decorator="['dealType']"
              placeholder="请选择交易类型">
            </a-select>
          </a-form-item> -->
        </a-form>
      </template>
      <!-- 操作栏 -->
      <template slot="oprationRow">
        <a-row>
          <a-col :span="24" style="text-align: right; font-size: 16px; color: #666;">
            总计: ¥ {{ (Number(this.record.money)/100).toFixed(2) }}元
          </a-col>
        </a-row>
      </template>
    </search-table>
    
    <template slot="footer">
      <a-button key="back" @click="closeModal(false)"> {{ handle == 'view' ? '返回' : '取消' }} </a-button>
      <a-button :loading="confirmLoading" v-if="handle != 'view'" key="submit" type="primary" @click="handleSubmit"> 提交 </a-button>
    </template>
  </a-modal>
</template>

<script>
import { findDictNameByValue,createOptionsByDict } from '@/utils/dict'
import deepCopy from '@/utils/deepCopy'

const formData={
  id: undefined,
  withdrawOrderNo: undefined, // 提现订单号
  auditorStatus: 2, // 审核结果
  remark: undefined, // 审核意见
  updateDate: undefined, // 审核时间
  auditor: undefined, // 审核人
}

export default {
  name: 'withdrawAuditForm',
  props: {
    handle: { // 操作类型
      type: String,
      default: 'edit'
    },
    record: { // 选中行数据
      type: Object,
      default () {
        return {}
      }
    },
    visible: { // 是否显示弹窗
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      confirmLoading: false, // 提交等待
      formData,
      disabled: false, // 表单禁用状态
      rules:{ // 表单验证规则
        auditorStatus: [
          { required: true, message: '请选择审核结果', trigger: 'change'},
        ],
        remark: [
          { required: true, message: '请输入审核意见', trigger: 'blur'},
        ],
      },
      url: '/order/fashionablelog/page', // 列表接口地址
      headParams: {}, // 请求头参数
      defaultParams: { // 默认携带参数
        auditorStatus: -1,
        withdrawOrderNo: undefined
      },
      dealTypeList: createOptionsByDict('DEAL_TYPE'), // 交易类型集合
      form: this.$form.createForm(this, { name: "table_search" }), // 搜索表单 
      columns: [
        {
          title: "时间",
          dataIndex: "createDate",
          key: "createDate",
        },
        {
          title: "类型",
          dataIndex: "type",
          key: "type",
          customRender: (text, record) =>  '收入'// findDictNameByValue('DEAL_TYPE', text)
        },
        {
          title: "交易名称",
          dataIndex: "dealname",
          key: "dealname",
          customRender: (text, record) =>  '订单收入'
        },
        {
          title: "商品名称",
          dataIndex: "goodsName",
          key: "goodsName",
        },
        {
          title: "订单号",
          dataIndex: "orderGoodsId",
          key: "orderGoodsId",
        },
        {
          title: "金额",
          dataIndex: "fashionablePrice",
          key: "fashionablePrice",
          customRender:(text,row)=>{
            return text ? '¥ '+(Number(text)/100).toFixed(2) : '¥ 0.00'
          }
        },
      ],
    };
  },
  computed: {
    title() { // 弹窗标题
      let res = ''
      switch (this.handle) {
        case 'add':
          res = '添加'
          break;
        case 'edit':
          res = '提现审核'
          break;
        case 'view':
          res = '查看提现'
          break;
        default:
          res = '提现审核'
          break;
        }
      return res
    }
  },
  watch: {
    // 监听弹窗显示，回显数据
    visible(value) {
      if (value) {
        this.$nextTick(()=> {
          this.record.auditorStatusStr = findDictNameByValue('WITHDRAW_AUDIT_STATUS', this.defaultParams.auditorStatus)
          this.formData = deepCopy(this.record)
          if(this.handle == 'view') {
            this.formData.auditorStatus = this.record.auditorStatus
          } else {
            this.formData.auditorStatus = 2
            this.formData.remark = undefined
            this.formData.updateDate = undefined
            this.formData.auditor = undefined
          }
        })
      } else {
        this.resetData()
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 重置
    resetData(){
      this.formData=deepCopy(formData);
    },
    // 提交
    handleSubmit() {
      let _this = this
      this.confirmLoading = true;
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.confirmLoading = false;
          this.formData.ids = this.chosenIds
          _this.$post('/withdraw/auditor/update', 
            this.formData)
          .then(res => {
            if (res && res.returnCode === 1000) {
              _this.confirmLoading = false;
              _this.$message.success('保存成功！')
              _this.closeModal(true)
            }
          })
          .catch(err => {
            _this.confirmLoading = false;
          })
        } else {
          this.confirmLoading = false;
          return false;
        }
      });
    },
    // 取消
    closeModal(bool) {
      this.chosenIds=[]
      this.chosenIdsPrice=[]
      this.allSelectedRows=[]
      this.$emit('cancel', bool)
    }
  }
};
</script>

<style lang="less">
.withdraw-audit-modal{
  .ant-descriptions-bordered .ant-descriptions-item-label{
    width: 150px;
  }
}
</style>
